{% from "macros.html" import render_pagination %}

<!DOCTYPE html>
<html>
<head>
    <title>客户管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <div class="container">
        <h1 class="page-title">客户管理</h1>
        <div class="nav-menu">
            <a href="{{ url_for('index') }}" class="nav-item">返回首页</a>
            <a href="#" class="nav-item add-btn" onclick="showModal()">新增客户</a>
        </div>
        
        <!-- 新增客户模态框 -->
        <div id="addModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="hideModal()">&times;</span>
                <h2>新增客户</h2>
                <form action="{{ url_for('add_customer') }}" method="POST">
                    <div class="form-group">
                        <label for="name">客户名称</label>
                        <input type="text" id="name" name="name" required>
                    </div>
                    <div class="form-group">
                        <label for="phone">联系电话</label>
                        <input type="text" id="phone" name="phone">
                    </div>
                    <button type="submit" class="nav-item add-btn">保存</button>
                </form>
            </div>
        </div>
        
        <!-- 编辑客户模态框 -->
        <div id="editModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="hideEditModal()">&times;</span>
                <h2>编辑客户</h2>
                <form action="{{ url_for('edit_customer') }}" method="POST">
                    <input type="hidden" id="edit_id" name="id">
                    <div class="form-group">
                        <label for="edit_name">客户名称</label>
                        <input type="text" id="edit_name" name="name" required>
                    </div>
                    <div class="form-group">
                        <label for="edit_phone">联系电话</label>
                        <input type="text" id="edit_phone" name="phone">
                    </div>
                    <button type="submit" class="nav-item add-btn">保存</button>
                </form>
            </div>
        </div>
        
        <div class="section">
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>客户名称</th>
                        <th>联系电话</th>
                        <th>车辆数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for customer in customers %}
                    <tr>
                        <td>{{ customer.id }}</td>
                        <td>{{ customer.name }}</td>
                        <td>{{ customer.phone }}</td>
                        <td>{{ customer.vehicles|length }}</td>
                        <td>
                            <a href="{{ url_for('customer_detail', id=customer.id) }}" class="action-btn" style="background-color: #17a2b8;">查看详情</a>
                            <a href="#" class="action-btn edit-btn" onclick="showEditModal('{{ customer.id }}', '{{ customer.name }}', '{{ customer.phone }}')">编辑</a>
                            <a href="#" class="action-btn delete-btn">删除</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            
            {{ render_pagination(pagination, 'customers') }}
        </div>
    </div>

    <script>
        function showModal() {
            document.getElementById('addModal').style.display = 'block';
        }
        
        function hideModal() {
            document.getElementById('addModal').style.display = 'none';
        }
        
        window.onclick = function(event) {
            var modal = document.getElementById('addModal');
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
        
        function showEditModal(id, name, phone) {
            document.getElementById('edit_id').value = id;
            document.getElementById('edit_name').value = name;
            document.getElementById('edit_phone').value = phone;
            document.getElementById('editModal').style.display = 'block';
        }
        
        function hideEditModal() {
            document.getElementById('editModal').style.display = 'none';
        }
    </script>
</body>
</html> 